import request from '@/utils/request'
import type { PagesData, ResponseData } from '@/api/type.ts'
import type {
  LoginForm,
  Userinfo,
  User,
  AllUserRoleData,
  SetRoleData,
} from '@/api/user/type.ts'

enum API {
  LOGIN_URL = '/admin/acl/index/login',
  USERINFO_URL = '/admin/acl/index/info',
  LOGOUT_URL = '/admin/acl/index/logout',
  ALL_USER_URL = '/admin/acl/user',
  ADD_USER_URL = '/admin/acl/user/save',
  UPDATE_USER_URL = '/admin/acl/user/update',
  ALL_ROLE_URL = '/admin/acl/user/toAssign',
  ASSIGN_ROLE_URL = '/admin/acl/user/doAssignRole',
  DELETE_USER_URL = '/admin/acl/user/remove',
  DELETE_USER_BATCH_URL = '/admin/acl/user/batchRemove',
}

export const reqLogin = (data: LoginForm) => {
  return request.post<any, ResponseData<string>>(API.LOGIN_URL, data)
}

export const reqUserInfo = () => {
  return request.get<any, ResponseData<Userinfo>>(API.USERINFO_URL)
}

export const reqLogout = () => {
  return request.post<any, ResponseData<any>>(API.LOGOUT_URL)
}

export const reqUserList = (page: number, limit: number, username: string) => {
  return request.get<any, ResponseData<PagesData<User>>>(
    API.ALL_USER_URL + `/${page}/${limit}?username=${username}`,
  )
}

export const reqUserAddOrUpdate = (user: User) => {
  if (user.id) {
    return request.put<any, ResponseData<any>>(API.UPDATE_USER_URL, user)
  } else {
    return request.post<any, ResponseData<any>>(API.ADD_USER_URL, user)
  }
}

export const reqAllRole = (userId: number) => {
  return request.get<any, ResponseData<AllUserRoleData>>(
    `${API.ALL_ROLE_URL}/${userId}`,
  )
}

export const reqAssignRole = (data: SetRoleData) => {
  return request.post<any, ResponseData<any>>(API.ASSIGN_ROLE_URL, data)
}

export const reqDeleteUser = (userId: number) => {
  return request.delete<any, ResponseData<any>>(
    API.DELETE_USER_URL + '/' + userId,
  )
}

export const reqDeleteUserBatch = (idList: number[]) => {
  return request.delete<any, ResponseData<any>>(API.DELETE_USER_BATCH_URL, {
    data: idList,
  })
}
